<script setup>
import { Edit, Delete } from "@element-plus/icons-vue";
import { ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
const warehouse = ref([
  {
    warehouseID: 3,
    name: "仓库1",
    address: "地址",
    contact: "联系人",
    contactNumber: 18810857781,
  },
]);

//声明一个异步的函数
import {
  warehouseListService,
  warehouseAddService,
  warehouseUpdateService,
  warehouseDeleteService,
  warehouseSearchService,
} from "@/api/warehouse.js";

const warehouseList = async () => {
  let result = await warehouseListService();
  // if(result.code===1){
  // warehouse.value = result.data;
  // }else{
  // ElMessage.error('获取数据失败')
  // }
  warehouse.value = result.data;
};
warehouseList();

//添加仓库的内容
//控制添加分类弹窗
const dialogVisible = ref(false);

//添加分类数据模型
const warehouseModel = ref({
  name: "",
  address: "",
  contact: "",
  contactNumber: 10000000000,
  warehouseID: 1,
});
//添加分类表单校验
const rules = {
  name: [{ required: true, message: "请输入仓库名称", trigger: "blur" }],
  address: [{ required: true, message: "请输入地址", trigger: "blur" }],
  contact: [{ required: true, message: "请输入联系人", trigger: "blur" }],
  contactNumber: [
    { required: true, message: "请输入联系方式", trigger: "blur" },
  ],
};

//调用接口，添加仓库
const addWarehouse = async () => {
  //调用接口
  let result = await warehouseAddService(warehouseModel.value);
  ElMessage.success(result.msg ? result.msg : "添加成功");

  //调用获取所有仓库的接口
  warehouseList();
  dialogVisible.value = false;
};

//定义变量，控制标题的展示
const title = ref("");
// 展示编辑弹窗
const showDialog = (row) => {
  dialogVisible.value = true;
  title.value = "编辑仓库";
  //将当前行的数据赋值给warehouseModel
  warehouseModel.value.name = row.name;
  warehouseModel.value.address = row.address;
  warehouseModel.value.contact = row.contact;
  warehouseModel.value.contactNumber = row.contactNumber;
  // 扩展id属性，将来需要传递给后台
  warehouseModel.value.warehouseID = row.warehouseID;
};

//编辑仓库
const updateWarehouse = async () => {
  // 调用接口
  let result = await warehouseUpdateService(warehouseModel.value);

  ElMessage.success(result.msg ? result.msg : "编辑成功");

  //调用获取所有仓库的接口
  warehouseList();
  dialogVisible.value = false;
};
//清空模型的数据
const clearData = () => {
  warehouseModel.value = {
    name: "",
    address: "",
    contact: "",
    contactNumber: 10000000000,
  };
};

// 删除仓库
const deleteWarehouse = (row) => {
  // 提示用户 确认删除吗？ 确认框
  ElMessageBox.confirm("你确认要删除该仓库吗？", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      //调用接口
      let result = await warehouseDeleteService(row.warehouseID);
      ElMessage({
        type: "success",
        message: "完成删除",
      });
      // 刷新列表
      warehouseList();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};

// 根据ID获取单个仓库
const searchWarehouse = async () => {
  let result = await warehouseSearchService(warehouseModel.value);
  ElMessage.success(result.msg ? result.msg : "查询成功");

  // warehouse.value[0] = result.data;
  // 清空 warehouse.value
  warehouse.value.splice(0);

  // 将搜索到的数据添加到列表中
  warehouse.value.push(result.data);
};
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>仓库</span>
        <div class="extra">
          <el-button
            type="primary"
            @click="
              dialogVisible = true;
              title = '添加仓库';
              clearData();
            "
            >添加仓库</el-button
          >
        </div>
      </div>
    </template>
    <el-form inline>
      <el-form-item label="仓库id：">
        <el-select placeholder="请选择" v-model="warehouseModel.warehouseID">
          <el-option
            v-for="c in warehouse"
            :key="c.warehouseID"
            :label="c.warehouseID"
            :value="c.warehouseID"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchWarehouse">搜索</el-button>
        <el-button @click="warehouseList">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="warehouse" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="仓库ID" prop="warehouseID"> </el-table-column>
      <el-table-column label="仓库名" prop="name"></el-table-column>
      <el-table-column label="地址" prop="address"></el-table-column>
      <el-table-column label="联系人" prop="contact"> </el-table-column>
      <el-table-column label="联系方式" prop="contactNumber"> </el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="showDialog(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="deleteWarehouse(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <!-- 添加分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form
        :model="warehouseModel"
        :rules="rules"
        label-width="100px"
        style="padding-right: 30px"
      >
        <el-form-item label="仓库名称" prop="name">
          <el-input
            v-model="warehouseModel.name"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="仓库地址" prop="address">
          <el-input
            v-model="warehouseModel.address"
            minlength="1"
            maxlength="15"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input
            v-model="warehouseModel.contact"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="contactNumber">
          <el-input
            v-model="warehouseModel.contactNumber"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="title == '添加仓库' ? addWarehouse() : updateWarehouse()"
          >
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>


<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .el-select {
    width: 150px;
  }
}
</style>